package bishi.aiqiyi;

import java.util.*;

/**
 * @author: 唐小尊
 * @email: 1171702529@qq.com
 * @cate: 2021/08/01 15:42
 */
public class two {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(rain(new int[]{
                1, 2, 0, 0, 2, 1
        })));
    }

    public static int[] rain(int[] rains) {
        int[] nums = new int[rains.length];
        Arrays.fill(nums, -1);
        Set<Integer> set = new HashSet<>();
        Deque<Integer> konk = new LinkedList<>();
        for (int i = 0; i < rains.length; i++) {
            if (rains[i] > 0) {
                if (set.contains(rains[i])) {
                    if (konk.size() > 0) {
                        nums[konk.pollLast()] = rains[i];
                    } else {
                        return new int[0];
                    }
                } else {
                    nums[i] = -1;
                    set.add(rains[i]);
                }
            } else {
                konk.offerLast(i);
            }
        }
        while (konk.size() > 0) {
            rains[konk.pollLast()] = 1;
        }
        return nums;
    }
}
